﻿using System;
using Uniloader.Managers;
using MonoTorrent.Client;
using System.IO;
using MonoTorrent.Common;

namespace XamlTorrent.Managers.MonoTorrent
{
    public class MonoTorrentTask : TransferTask
    {
        private TorrentManager manager;
        private FileSystemInfo localDest;

        public MonoTorrentTask(TorrentManager mgr, FileSystemInfo destination)
        {
            manager = mgr;
            localDest = destination;
        }

        public Torrent Torrent
        {
            get { return manager.Torrent; }
        }

        public TorrentManager TorrentManager
        {
            get { return manager; }
        }

        public override string DisplayName
        {
            get { return manager.Torrent.Name; }
        }

        public override FileSystemInfo LocalData
        {
            get { return localDest; }
        }

        public override TimeSpan RemainingTime
        {
            get { return TimeSpan.Zero; }
        }

        public override object TransmisionMetadata
        {
            get { return manager.Torrent; }
        }

        public override void Start()
        {
            manager.Start();
        }

        public override void Stop()
        {
            manager.Stop();
        }

        public override void Suspend()
        {
            manager.Pause();
        }

        public override bool PauseSupported
        {
            get { return true; }
        }

        public override TaskState TaskState
        {
            get 
            {
                switch (manager.State)
                {
                    case TorrentState.Stopped:
                        return TaskState.Stopped;

                    case TorrentState.Downloading:
                    case TorrentState.Seeding:
                        return TaskState.Transferring;

                    case TorrentState.Hashing:
                        return TaskState.Initializing;

                    case TorrentState.Paused:
                        return TaskState.Paused;

                    default: 
                        return TaskState.Unknown;
                }
            }
        }

        public override long BytesSendTarget
        {
            get { return manager.Torrent.Size; }
        }

        public override long BytesReceiveTarget
        {
            get { return manager.Torrent.Size; }
        }

        public override long BytesSent
        {
            get { return 0; }
        }

        public override long BytesReceived
        {
            get { return 0; }
        }

        public override double Progress
        {
            get { return manager.Progress; }
        }
    }
}
